package com.advertising.controls;

import java.util.List;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.advertising.biz.IReleaseService;
import com.advertising.model.CloneRelease;
import com.advertising.model.Release;
import com.advertising.permissions.biz.IUserService;
import com.advertising.permissions.model.Users;
import com.tgyt.common.tools.page.Pagination;
import com.tgyt.framework.controls.struts2.BaseTg;

@Scope("prototype")
@Controller(value = "releaseTgControl")
public class ReleaseTg extends BaseTg {

	private Pagination pagination;

	private CloneRelease release;
	
	private String comtel;//传过的电话号吗

	private Integer id;
	
	private String serchKey;
	
	private String linkerID;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private IReleaseService releaseService;
	@Autowired
	private IUserService userService;

	public String index() {
		return SUCCESS;
	}

	/**
	 * 已发布界面
	 * 
	 * @return
	 */
	public String already() {
		return SUCCESS;

	}

	/**
	 * 发布中
	 * 
	 * @return
	 */
	public String release() {
		return SUCCESS;
	}

	/**
	 * 发布中
	 * 
	 * @return
	 */
	public String add() {
		return SUCCESS;
	}

	/**
	 * @Title: getItems
	 * @Description:广告列表
	 * @param
	 * @return void
	 * @throws
	 */
	@SuppressWarnings("unchecked")
	public void getItems() {
		try {
			Pagination pagination = this.releaseService.getPageList(
					releaseService.convert(release), page, rows, sort, order);
			List<Release> list = pagination.getList();
			String str = "";
			if (list.size() != 0) {
				str = JSONArray.fromObject(releaseService.convert(list))
						.toString();
			} else {
				str = "[]";
			}
			outJsonPlainString(response,
					"{\"total\":" + pagination.getTotalCount() + ",\"rows\":"
							+ str + "}");
		} catch (Exception e) {
			e.printStackTrace();
			outJsonPlainString(response, "{\"total\":0, \"rows\":[]}");
		}
	}

	/**
	 * @Title: save
	 * @Description: 保存一条资源记录
	 * @param
	 * @return void
	 * @throws
	 */
	public void save() {
		try {

			this.releaseService.save(releaseService.convert(release));
			outJsonPlainString(response, "{\"success\":true}");
		} catch (Exception e) {
			e.printStackTrace();
			outJsonPlainString(response, "{\"error\":true}");
		}
	}

	/**
	 * @Title: update
	 * @Description:修改资源信息
	 * @param
	 * @return void
	 * @throws
	 */
	public void update() {
		try {
			boolean s = this.releaseService.alter(releaseService
					.convert(release));
			// request.setAttribute("allTrees",this.releaseService.getAllHTMLTrees(request));
			outJsonPlainString(response, "{\"success\":true}");
		} catch (Exception e) {
			e.printStackTrace();
			outJsonPlainString(response, "{\"error\":true}");
		}
	}

	/**
	 * @Title: delete
	 * @Description:删除
	 * @param
	 * @return void
	 * @throws
	 */
	public void delete() {
		try {
			this.releaseService.deleteById(id);
			// request.setAttribute("allTrees",this.resourcesService.getAllHTMLTrees(request));
			outJsonPlainString(response, "{\"success\":true}");
		} catch (Exception e) {
			e.printStackTrace();
			outJsonPlainString(response, "{\"error\":true}");
		}
	}

	public String getComImage() {
		Users users = userService.findBytel(comtel);
		String result = releaseService.findImageUrl(users.getId()+"");
		return result;
	}

	public String serch(){
		String str = "";
		try {
			
			release.setUserid(linkerID);
			release.setIntroduction(serchKey);
			Pagination pagination = this.releaseService.getPageList(
					releaseService.convert(release), page, rows, sort, order);
			List<Release> list = pagination.getList();
			
			if (list.size() != 0) {
				str = JSONArray.fromObject(releaseService.convert(list))
						.toString();
			} else {
				str = "[]";
			}

		} catch (Exception e) {
		   
			e.printStackTrace();
		}
		return str;
	}
	
	// ///////////////////set get ////////////////////////////

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	public CloneRelease getRelease() {
		return release;
	}

	public void setRelease(CloneRelease release) {
		this.release = release;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getComtel() {
		return comtel;
	}

	public void setComtel(String comtel) {
		this.comtel = comtel;
	}

	public String getSerchKey() {
		return serchKey;
	}

	public void setSerchKey(String serchKey) {
		this.serchKey = serchKey;
	}

	public String getLinkerID() {
		return linkerID;
	}

	public void setLinkerID(String linkerID) {
		this.linkerID = linkerID;
	}

}
